package com.tms.app.tms.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.test.db.MyMessage;
import com.tms.app.tms.test.service.AlarmReceiver;

import org.xutils.DbManager;
import org.xutils.x;

import java.util.Date;

/**
 * Created by Administrator on 2017/10/24 0024.
 */

public class AlarmService extends Service {
    private AlarmManager am;
    private PendingIntent pi;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();

    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        getAlarmTime();
        return START_REDELIVER_INTENT; //这里为了提高优先级，选择START_REDELIVER_INTENT 没那么容易被内存清理时杀死
    }

    public void getAlarmTime() {
        MyMessage myMessage=null;
        try {
            DbManager   db = x.getDb(MyApp.getDaoConfig());
           myMessage = db.selector(MyMessage.class).findFirst();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
//        if (myMessage==null){
//            myMessage=new MyMessage();
//            myMessage.setContent("提醒测试");
//            myMessage.setTitle("提醒测试");
//            myMessage.setRemindTime(new Date().getTime()+5000);
//        }
//        Intent startNotification = new Intent(this, AlarmReceiver.class);   //这里启动的广播，下一步会教大家设置
//        startNotification.putExtra("title", myMessage.getTitle());
//        startNotification.putExtra("content", myMessage.getContent());
//        startNotification.setAction("HL");
//        am = (AlarmManager) getSystemService(ALARM_SERVICE);   //这里是系统闹钟的对象
//        pi = PendingIntent.getBroadcast(this, 0, startNotification, PendingIntent.FLAG_UPDATE_CURRENT);     //设置事件
//        if (myMessage.getRemindTime() >0) {
//            am.set(AlarmManager.RTC_WAKEUP, myMessage.getRemindTime(), pi);    //提交事件，发送给 广播接收器
//        } else {
//            //当提醒时间为空的时候，关闭服务，下次添加提醒时再开启
//            stopService(new Intent(this, AlarmService.class));
//        }
    }




}